﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Controls_Basket : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Product_ID"] != null)
                AddProductToBasket();
        }
    }
    private void AddProductToBasket()
    {
        Product product = Product_Session.Get_Product_ByID(int.Parse(Request.QueryString["Product_ID"]));
        if (product == null) return;
        OrderProduct item = new OrderProduct();
        item.OrderProductID = Convert.ToString(product.Pro_ID);
        item.ProName = product.Pro_Name;
        item.Image = product.Pro_Image;
        item.Price = Convert.ToSingle(product.Pro_Price);
        ManageBasket.InsertProductToBasket(item);

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {

            GridViewRow selectedRow = GridView1.Rows[int.Parse(e.CommandArgument.ToString())];

            TableCell lastNameCell = selectedRow.Cells[0];
            OrderProduct orderproduct = new OrderProduct();
            orderproduct.OrderProductID = lastNameCell.Text.Trim();
            ManageBasket.DeleteProductFromBasket(orderproduct);

            GridView1.DataBind();
           // SetTotalCout();
            Response.Redirect("Basket.aspx");

        }
        if (e.CommandName == "Edit")
        {
            GridViewRow selectedRow = GridView1.Rows[int.Parse(e.CommandArgument.ToString())];


            TableCell lastNameCell = selectedRow.Cells[0];
            OrderProduct orderproduct = ManageBasket.GetProductFromBasket(lastNameCell.Text.Trim());
            if (orderproduct == null) return;



            TextBox txt = (TextBox)selectedRow.FindControl("soluong");
            OrderProduct newProduct = orderproduct;
            newProduct.Amount = int.Parse(txt.Text);


            ManageBasket.UpdateProductFromBasket(orderproduct, newProduct);
            GridView1.DataBind();
            Response.Redirect("Basket.aspx");
//            SetTotalCout();


        }
    }

    protected void continue_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    /*
    protected void btndathang_Click(object sender, EventArgs e)
    {
        Invetory inventory = new Invetory();
        string s = Guid.NewGuid().ToString();
        inventory.Inv_ID = s;
        inventory.Inv_CreateDate = DateTime.Now;
        inventory.Inv_TotalPrice = ManageBasket.Basket.Summary;
        inventory.Inv_TotalQuantity = ManageBasket.Basket.Count;
        inventory.Int_Status = 0;
        inventory.User_ID = 1;
        inventory.Inv_UserRevice = txthoten.Text;
        inventory.Inv_UserRevicePhone = txtdienthoai.Text;
        inventory.Inv_UserReviceAddress = txtdiachi.Text;
        inventory.Inv_PayType = int.Parse(drlhinhthucthanhtoan.SelectedValue);
        inventory.Inv_DateRevice = Convert.ToDateTime(txtthoigiangiaohang.Text);
        inventory.Local_ID = int.Parse(drldiadiemgiaohang.SelectedValue);
        inventory.Inv_Note = txtghichu.Text;
        inventory.Inv_UserReviceEmail = txtEmail.Text;

        Invetory_session.Insert_Invetory(inventory);
        ManageBasket.InsertProductIntoTable(s);
        Response.Redirect("Default.aspx");
    }*/
    protected void Payment_Click(object sender, EventArgs e)
    {
        Response.Redirect("Payment.aspx");
    }
}
